<?php

namespace app\common\utils;

use app\common\model\common\CommonConfig;

/**
 * 工具类
 */
class MyTools
{
    /**
     * 获取真实的客户端IP地址
     */
    public static function getRealIP()
    {
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = @$_SERVER['REMOTE_ADDR'] ?: '';
        }
        return $ip;
    }

    /**
     * 获取随机字符串
     * @return string
     */
    public static function getRandomStr()
    {
        $strs = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
        return substr(str_shuffle($strs), mt_rand(0, strlen($strs) - 11), 15);
    }

    /**
     * 生成指定长度的随机汉字
     * @param integer $length 长度
     * @return string
     */
    public static function getChineseRandomStr($length)
    {
        $chinese = '';
        for ($i = 0; $i < $length; $i++) {
            $chr = chr(mt_rand(0xB0, 0xD0)) . chr(mt_rand(0xA1, 0xF0));
            $chinese .= iconv('GB2312', 'UTF-8', $chr);
        }
        return $chinese;
    }

    /**
     * 生成随机的中国人姓名
     * @return string
     */
    public static function getChineseRandomName()
    {
        $lastname_arr = explode(',', CommonConfig::getValueByKey('chinese_last_name'));
        $firstname_arr = explode(',', CommonConfig::getValueByKey('chinese_first_name'));
        if (empty($lastname_arr) || empty($firstname_arr)) {
            return '';
        }

        //获取第一位
        $key1 = rand(0, count($lastname_arr) - 1);
        $one_name = $lastname_arr[$key1];

        //获取第二位
        $key2 = rand(0, count($firstname_arr) - 1);
        $two_name = $firstname_arr[$key2];

        //获取第三位
        $key3 = rand(0, count($firstname_arr) - 1);
        $three_name = $firstname_arr[$key3];

        return $one_name . $two_name . $three_name;
    }

}